<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Web Solutions
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2005/08/07 05:20:29 $)
// $RCSfile: functions_adtracking.php,v $ : $Revision: 1.4 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================


if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

/**
* Add Hit Log for specified Campaign
*/
function addHitLog($campaignid)
{
	global $dbCore, $_SWIFT;

	$_browser = @get_browser();
	if (empty($_browser))
	{
		$_browser = $_SERVER["HTTP_USER_AGENT"];
	}

	$dbCore->shutdownQuery("INSERT INTO `". TABLE_PREFIX ."adhitlogs` (`campaignid`, `dateline`, `referrer`, `browser`, `platform`, `ipaddress`) VALUES ('". $dbCore->escape($campaignid) ."', '". DATENOW ."', '". $dbCore->escape($_SERVER["HTTP_REFERER"]) ."', '". $dbCore->escape($_browser->parent) ."', '". $dbCore->escape($_browser->platform) ."', '". $dbCore->escape($_SERVER["REMOTE_ADDR"]) ."');");
}

/**
* Adds a new campaign
*/
function insertCampaign($title, $redirecturl)
{
	global $dbCore, $_SWIFT;

	$dbCore->query("INSERT INTO `". TABLE_PREFIX ."adcampaigns` (`title`, `redirecturl`, `dateline`) VALUES ('". $dbCore->escape($title) ."', '". $dbCore->escape($redirecturl) ."', '". DATENOW ."');");
	$campaignid = $dbCore->lastInsertId();

	rebuildCampaignCache();

	return $campaignid;
}

/**
* Renders the Campaign Insert/Edit form
*/
function renderCampaignForm($type)
{
	global $_SWIFT;

	printFormStart();
	printMainTableHeader($_SWIFT["language"]["insertcampaign"]);

	printTextRow("title", $_SWIFT["language"]["campaigntitle"], $_SWIFT["language"]["desc_campaigntitle"], "text", $_POST["title"]);

	printTextRow("redirecturl", $_SWIFT["language"]["redirecturl"], $_SWIFT["language"]["desc_redirecturl"], "text", $_POST["redirecturl"]);

	printSubmitRow(iif($type==INSERT,$_SWIFT["language"]["insert"],$_SWIFT["language"]["update"]));

	printMainTableFooter();

	addHiddenField("_m", "livesupport");
	addHiddenField("_a", iif($type==INSERT,"insertcampaign","editcampaign"));
	addHiddenField("step", "1");
	if ($type == EDIT)
	{
		addHiddenField("campaignid", $_POST["campaignid"]);
	}

	printFormEnd();
}

/**
* Deletes an existing ad tracking campaign
*/
function deleteCampaigns($campaignidlist)
{
	global $dbCore;

	if (!is_array($campaignidlist) || !count($campaignidlist))
	{
		return false;
	}

	$dbCore->query("DELETE FROM `". TABLE_PREFIX ."adcampaigns` WHERE `campaignid` IN (". buildIN($campaignidlist) .");");
	$dbCore->query("DELETE FROM `". TABLE_PREFIX ."adhitlogs` WHERE `campaignid` IN (". buildIN($campaignidlist) .");");

	rebuildCampaignCache();

	return true;
}

/**
* Clears the campaign hit log
*/
function deleteHitLog($campaignidlist)
{
	if (!is_array($campaignidlist) || !count($campaignidlist))
	{
		return false;
	}

	$dbCore->query("DELETE FROM `". TABLE_PREFIX ."adhitlogs` WHERE `campaignid` IN (". buildIN($campaignidlist) .");");

	return true;
}

/**
* Updates campaign data
*/
function updateCampaign($campaignid, $title, $redirecturl)
{
	global $dbCore;

	$dbCore->query("UPDATE `". TABLE_PREFIX ."adcampaigns` SET `title` = '". $dbCore->escape($title) ."', `redirecturl` = '". $dbCore->escape($redirecturl) ."' WHERE `campaignid` = '". $dbCore->escape($campaignid) ."';");

	rebuildCampaignCache();

	return true;
}

/**
* Rebuilds the ad tracking campaign cache
*/
function rebuildCampaignCache()
{
	global $dbCore, $datastore;

	$campaigncache = array();
	$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."adcampaigns` ORDER BY `campaignid` ASC;");
	while ($dbCore->nextRecord())
	{
		$campaigncache[$dbCore->Record["campaignid"]] = $dbCore->Record;
	}

	$datastore->update("campaigncache", $campaigncache);
}
?>